/* ============================================================================= */
/*                           TABLE 5 - IFP ANALYSIS                              */
/* ============================================================================= */

clear all

capture log close
log using "$logs\Table_5_IFP.log", replace

capture estimates drop *

/* ------------------------------------------------------------------------------
   Step 1: Loan Types Decomposition - Long-Term vs. Other Loans
------------------------------------------------------------------------------ */

use "$output\matched_sample_nn.dta", clear

    keep if mdate < mofd(date("20200101","YMD"))
    keep if mdate > mofd(date("20131231","YMD"))

    keep if deltamonths >= -12 & deltamonths <= 24

    * Compute loan logs
    gen lltloan = log(ltloan + bm + bi)   // Long-term loans
    gen lco = log(co)                     // Credit line
    gen lother = log(stloan + oc)         // Other loans

    * Long-term loan regression
    qui eststo regltloan: reghdfe lltloan i1.p2p##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren) 
    estadd local yearfirm "Y", replace   
    estadd local mdateFE "Y", replace  

    * Other loan regression
    qui eststo reglother: reghdfe lother i1.p2p##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren) 
    estadd local yearfirm "Y", replace   
    estadd local mdateFE "Y", replace  

/* ------------------------------------------------------------------------------
   Step 2: Investment Loans Only
------------------------------------------------------------------------------ */

use "$output\matched_sample_nn_forinvestment_1.dta", clear

    keep if mdate < mofd(date("20200101","YMD"))
    keep if mdate > mofd(date("20131231","YMD"))
    keep if deltamonths >= -12 & deltamonths <= 24

    gen loan = stloan + ltloan + bm + bi + oc
    sort siren mdate
    gen lloan = log(loan)

    * Investment loan regression
    eststo reginvest: reghdfe lloan i1.p2p##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren)
    estadd local yearfirm "Y", replace 
    estadd local mdateFE "Y", replace  

/* ------------------------------------------------------------------------------
   Step 3: Non-Investment Loans
------------------------------------------------------------------------------ */

use "$output\matched_sample_nn_forinvestment_0.dta", clear

    keep if mdate < mofd(date("20200101","YMD"))
    keep if mdate > mofd(date("20131231","YMD"))
    keep if deltamonths >= -12 & deltamonths <= 24

    gen loan = stloan + ltloan + bm + bi + oc
    sort siren mdate
    gen lloan = log(loan)

    * Non-investment loan regression
    eststo regnotinvest: reghdfe lloan i1.p2p##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren)
    estadd local yearfirm "Y", replace 
    estadd local mdateFE "Y", replace  

/* ============================================================================= */
/*               EXPORT TABLE 5a TO LaTeX                              */
/* ============================================================================= */


esttab regltloan reglother reginvest regnotinvest using "$tables\Table_5_IFP_final.tex", replace ///
    noomitted nobase fragment gap booktabs label nonote noobs nolines /// 
    b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) /// 
    mtitles("Long-term loans" "Other loans" "For investment" "Not for investment") ///
    order(1.p2p#1.post 1.post) ///
    varlabel(1.post "Post" 1.p2p#1.post `"FinTech $\times$ Post"') ///
    stats(yearfirm mdateFE N r2, ///
        label(`"Firm-Year FE"' `"Month FE"' `"\midrule N"' `"R-sq"') ///
        layout("\multicolumn{1}c{@}") ///
        fmt(%9.0fc %9.0fc %9.0fc %9.2f))

log close
